import Vue from 'vue'
import VueRouter from 'vue-router'

// 引入不用懒加载的路由
import Layout from '@/Layout/Layout.vue'
import Pagenotfound from '@/other/Pagenotfound.vue'
const originalPush = VueRouter.prototype.push

VueRouter.prototype.push = function push(location) {
    return originalPush.call(this, location).catch(err => err)
}


Vue.use(VueRouter)

const routes = [{
        name: "login",
        path: "/login",
        component: () =>
            import ("@/views/users/login.vue"),
        meta: {
            animate: true,
            title: '用户登录'
        },
        alias: "/",
    }, {
        name: "Layout",
        path: "/Layout",
        component: Layout,
        // Layout子路由
        children: [{
            name: "welcome",
            path: "welcome",
            component: () =>
                import ("@/views/home/welcome.vue"),
            meta: {
                animate: true,
                title: '欢迎页'
            },
        }, {
            name: "users",
            path: "users",
            component: () =>
                import ("@/views/users/users.vue"),
            meta: {
                animate: true,
                title: '用户列表页'
            },
        }, {
            name: "adduser",
            path: "adduser",
            component: () =>
                import ("@/views/users/adduser.vue"),
            meta: {
                animate: true,
                title: '用户列表页'
            },
        }, {
            name: "edituser",
            path: "edituser/:userid",
            component: () =>
                import ("@/views/users/edituser.vue"),
            meta: {
                animate: true,
                title: '编辑用户信息'
            },
        }, {
            name: "orders",
            path: "orders",
            component: () =>
                import ("@/views/order/orders.vue"),
            meta: {
                animate: true,
                title: '用户列表页'
            },
        }, {
            name: "commodity",
            path: "commodity",
            component: () =>
                import ("@/views/commodity/commodity.vue"),
            meta: {
                animate: true,
                title: '用户列表页'
            },
        }]
    },
    {
        name: '404',
        path: '/404',
        component: Pagenotfound,
        meta: {
            animate: true
        }
    },
    {
        path: '*',
        redirect: '/404'
    }
];

const router = new VueRouter({
    routes
})

router.beforeEach((to, from, next) => {
    if (to.path === '/login') next();
    else {
        // 如果不是登录页面，判断当前浏览器中是否有登录凭证
        let userinfo = localStorage.getItem('userinfo')
        if (userinfo) {
            userinfo = JSON.parse(userinfo)
            if (userinfo.token) next()
            else next('/login?msg=当前身份已过期，请重新登录')
        } else next('/login?msg=请登录后访问该系统')
    }
});

router.afterEach((to, from) => {
    let title = to.meta.title
    if (title) document.title = `万和超市管理系统 - ${title}`
    else document.title = `万和超市管理系统`
})

export default router